package atm.view;

import atm.Atm;
import atm.domain.Card;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class MenuController {

    private Atm atm;
    @FXML
    private TextField t;

    @FXML
    private TextField cash;

    @FXML
    private TextArea textArea1;

    @FXML
    private TextArea textArea2;


    @FXML
    public void withdrawHandler() {
        String amountStr = t.getText();
        int amountInt = Integer.valueOf(amountStr);
        String cardId = this.atm.getCard().getCardid();

        if (this.atm.withdraw(new Card(cardId), amountInt)) {
            this.cash.setEditable(false);
            this.textArea1.setEditable(false);
            this.cash.setText(String.valueOf(this.atm.spitCash(amountInt).getCashNum()) + "元");
            this.textArea1.appendText("\n本次取得金额：" + this.atm.showInfo(amountInt) + "元");

        } else {

            this.textArea1.appendText("\n取款失败！金额必须是100的倍数！\n" + "或者ATM/账户余额不足");
        }


    }

    @FXML
    public void printReceipt() {
        String amountStr = t.getText();
        int amountInt = Integer.valueOf(amountStr);
        textArea2.setEditable(false);
        this.textArea2.setText(String.valueOf(this.atm.print(this.atm.getCard(), amountInt).getMsg()));
    }

    @FXML
    public void exit() {
        atm.loadLoginView();
    }

    public void setAtm(Atm atm) {
        this.atm = atm;
    }

}
